package io.renren.sf.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.sf.entity.CbBookEntity;
import io.renren.sf.service.CbBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.entity.CbBookEditionEntity;
import io.renren.sf.service.CbBookEditionService;
import io.renren.common.utils.R;



/**
 *
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-03-31 13:04:11
 */
@RestController
@RequestMapping("sf/cbbookedition")
public class CbBookEditionController {
    @Autowired
    private CbBookService cbBookService;
    @Autowired
    private CbBookEditionService cbBookEditionService;

    /**
     * 软笔出版社分页
     */
    @GetMapping("/getEditionList")
    public R getEditionList() {
        List<CbBookEditionEntity> editionList = cbBookEditionService.list();
        return R.ok().put("editionList", editionList);
    }
    /**
     * 软笔出版社分页
     */
    @GetMapping("/list")
    public R bookEditionPage(@RequestParam Map<String, Object> params) {
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        String editionName = (String) params.get("editionName");
        String isClassify = (String) params.get("isClassify");
        Page<CbBookEditionEntity> bookEditionPage = new Page<>(page, limit);
        QueryWrapper<CbBookEditionEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(editionName)) {
            queryWrapper.like("edition_name", editionName);
        }
        if(!StringUtils.isEmpty(isClassify)) {
            queryWrapper.eq("is_classify", Integer.parseInt(isClassify));
        }
        cbBookEditionService.page(bookEditionPage, queryWrapper);
        return R.ok().put("bookEditionPage", bookEditionPage);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbbookedition:info")
    public R info(@PathVariable("id") Long id){
		CbBookEditionEntity cbBookEdition = cbBookEditionService.getById(id);

        return R.ok().put("edition", cbBookEdition);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbbookedition:save")
    public R save(@RequestBody CbBookEditionEntity cbBookEdition){
        CbBookEditionEntity editionName = cbBookEditionService.getOne(new QueryWrapper<CbBookEditionEntity>().eq("edition_name", cbBookEdition.getEditionName()).last("limit 1"));
        if(editionName != null){
            return R.error("出版社名称已存在");
        }
        cbBookEdition.setCreatetime(new Date());
		cbBookEditionService.save(cbBookEdition);
        return R.ok().put("edition", cbBookEdition);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbbookedition:update")
    public R update(@RequestBody CbBookEditionEntity cbBookEdition){
		cbBookEditionService.updateById(cbBookEdition);
        return R.ok().put("edition", cbBookEdition);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbbookedition:delete")
    public R delete(@RequestBody Long[] ids){
        /*List<CbBookEntity> list = cbBookService.list(new QueryWrapper<CbBookEntity>().eq("eid", ids[0]));
        if(!CollectionUtils.isEmpty(list)){
            return R.error("该出版社下还有年级等信息未删除");
        }*/
		cbBookEditionService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
